﻿
package com.zhbit.UI;

import java.awt.CheckboxGroup;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.table.DefaultTableModel;

/**
 * 管理界面
 * @author ZWZ <ZHBIT at 2998732468@qq.com>
 */
public class BookManagerView extends javax.swing.JFrame {

    /**
     * Creates new form BookManagerView
     */
    private DefaultTableModel model;
    private String isRadioGroupSelect = "false";

    public BookManagerView() {
        initComponents();
        initData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupSatus = new javax.swing.ButtonGroup();
        jLabelBookID = new javax.swing.JLabel();
        jLabelBookName = new javax.swing.JLabel();
        jLabelAuthor = new javax.swing.JLabel();
        jLabelPressDate = new javax.swing.JLabel();
        jTextFieldBookID = new javax.swing.JTextField();
        jTextFieldBookName = new javax.swing.JTextField();
        jTextFieldAuthor = new javax.swing.JTextField();
        jTextFieldPressDate = new javax.swing.JTextField();
        jLabelPress = new javax.swing.JLabel();
        jTextFieldPress = new javax.swing.JTextField();
        jLabelStatus = new javax.swing.JLabel();
        jRadioButtonOn = new javax.swing.JRadioButton();
        jRadioButtonOut = new javax.swing.JRadioButton();
        jButtonLeft = new javax.swing.JButton();
        jButtonRight = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jButtonUpdate = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableDB = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelBookID.setText("图书编号：");

        jLabelBookName.setText("图书名称：");

        jLabelAuthor.setText("作    者：");

        jLabelPressDate.setText("出版日期：");

        jTextFieldBookID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldBookIDActionPerformed(evt);
            }
        });

        jTextFieldBookName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldBookNameActionPerformed(evt);
            }
        });

        jLabelPress.setText("出 版 社：");

        jLabelStatus.setText("在库状态：");

        jRadioButtonOn.setText("在库");
        jRadioButtonOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonOnActionPerformed(evt);
            }
        });

        jRadioButtonOut.setText("借出");
        jRadioButtonOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonOutActionPerformed(evt);
            }
        });

        jButtonLeft.setText("<");

        jButtonRight.setText(">");

        jButtonAdd.setText("添加");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonUpdate.setText("修改");
        jButtonUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateActionPerformed(evt);
            }
        });

        jButtonDelete.setText("删除");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButtonSave.setText("保存");

        jTableDB.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "图书编号", "图书名称", "作者", "出版社", "出版日期", "在库状态"
            }
        ));
        jTableDB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDBMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableDB);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonLeft)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonRight))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonAdd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonUpdate)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonDelete)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSave)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelBookID)
                                    .addComponent(jLabelBookName)
                                    .addComponent(jLabelAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelPressDate))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextFieldBookID)
                                            .addComponent(jTextFieldAuthor)
                                            .addComponent(jTextFieldPressDate))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabelStatus)
                                                .addGap(18, 18, 18)
                                                .addComponent(jRadioButtonOn)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jRadioButtonOut))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabelPress)
                                                .addGap(18, 18, 18)
                                                .addComponent(jTextFieldPress, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addComponent(jTextFieldBookName, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelBookID)
                    .addComponent(jTextFieldBookID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelBookName)
                    .addComponent(jTextFieldBookName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextFieldAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPress)
                    .addComponent(jTextFieldPress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPressDate)
                    .addComponent(jTextFieldPressDate)
                    .addComponent(jLabelStatus)
                    .addComponent(jRadioButtonOn)
                    .addComponent(jRadioButtonOut))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonLeft)
                    .addComponent(jButtonRight))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAdd)
                    .addComponent(jButtonUpdate)
                    .addComponent(jButtonDelete)
                    .addComponent(jButtonSave))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //添加按钮监听
    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        model.insertRow(model.getRowCount(), new Object[]{jTextFieldBookID.getText(), jTextFieldBookName.getText(),
            jTextFieldAuthor.getText(), jTextFieldPress.getText(), jTextFieldPressDate.getText(),isRadioGroupSelect});
    }//GEN-LAST:event_jButtonAddActionPerformed

    private void jTextFieldBookIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldBookIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldBookIDActionPerformed

    private void jTextFieldBookNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldBookNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldBookNameActionPerformed

    private void jRadioButtonOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonOnActionPerformed
        isRadioGroupSelect = jRadioButtonOn.getText();
    }//GEN-LAST:event_jRadioButtonOnActionPerformed

    private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateActionPerformed
        model.setValueAt(jTextFieldBookID.getText(), jTableDB.getSelectedRow(), 0);
        model.setValueAt(jTextFieldBookName.getText(), jTableDB.getSelectedRow(), 1);
        model.setValueAt(jTextFieldAuthor.getText(), jTableDB.getSelectedRow(), 2);
        model.setValueAt(jTextFieldPress.getText(), jTableDB.getSelectedRow(), 3);
        model.setValueAt(jTextFieldPressDate.getText(), jTableDB.getSelectedRow(), 4);
        model.setValueAt(isRadioGroupSelect, jTableDB.getSelectedRow(), 5);
        
    }//GEN-LAST:event_jButtonUpdateActionPerformed

    private void jTableDBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDBMouseClicked
        jTextFieldBookID.setText(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 0)));
        jTextFieldBookName.setText(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 1)));
        jTextFieldAuthor.setText(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 2)));
        jTextFieldPress.setText(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 3)));
        jTextFieldPressDate.setText(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 4)));
        
        if ("借出".indexOf(String.valueOf(model.getValueAt(jTableDB.getSelectedRow(), 5))) > -1) {
            jRadioButtonOut.setSelected(true);
        } else {
            jRadioButtonOn.setSelected(true);
        }
        
    }//GEN-LAST:event_jTableDBMouseClicked

    private void jRadioButtonOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonOutActionPerformed
        isRadioGroupSelect = jRadioButtonOut.getText();
    }//GEN-LAST:event_jRadioButtonOutActionPerformed

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        model.removeRow(jTableDB.getSelectedRow());
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BookManagerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BookManagerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BookManagerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BookManagerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BookManagerView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupSatus;
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonLeft;
    private javax.swing.JButton jButtonRight;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JButton jButtonUpdate;
    private javax.swing.JLabel jLabelAuthor;
    private javax.swing.JLabel jLabelBookID;
    private javax.swing.JLabel jLabelBookName;
    private javax.swing.JLabel jLabelPress;
    private javax.swing.JLabel jLabelPressDate;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JRadioButton jRadioButtonOn;
    private javax.swing.JRadioButton jRadioButtonOut;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableDB;
    private javax.swing.JTextField jTextFieldAuthor;
    private javax.swing.JTextField jTextFieldBookID;
    private javax.swing.JTextField jTextFieldBookName;
    private javax.swing.JTextField jTextFieldPress;
    private javax.swing.JTextField jTextFieldPressDate;
    // End of variables declaration//GEN-END:variables

    private void initData() {
    
        //获取GroupRadioButton的选择状态
        buttonGroupSatus.add(jRadioButtonOn);
        buttonGroupSatus.add(jRadioButtonOut);
        
        //绑定数据Table
         model = (DefaultTableModel) jTableDB.getModel();
    }
}
